package org.sws.domain.activity.service.quota;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.sws.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import org.sws.domain.activity.model.entity.*;
import org.sws.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import org.sws.domain.activity.model.valobj.UserAccountCountFlagVO;
import org.sws.domain.activity.repository.IActivityRepository;
import org.sws.domain.activity.service.quota.policy.TradePolicyContext;
import org.sws.domain.activity.service.quota.rule.factory.ActivityChainFactory;

import java.util.Date;
import java.util.List;

/**
 * @Author sws
 * @Date 2025/3/18 21:21
 * @description: 活动服务实现子类
 */
@Service
public class RaffleActivityServiceQuotaService extends AbstractRaffleActivityQuotaService implements IActivitySkuStockService {


    public RaffleActivityServiceQuotaService(IActivityRepository activityRepository, ActivityChainFactory activityChainFactory, TradePolicyContext tradePolicyContext) {
        super(activityRepository, activityChainFactory, tradePolicyContext);
    }

    @Override
    protected CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        // 活动额度单实体对象
        ActivityRechargeEntity activityRechargeEntity = new ActivityRechargeEntity();
        activityRechargeEntity.setUserId(skuRechargeEntity.getUserId());
        activityRechargeEntity.setSku(skuRechargeEntity.getSku());
        activityRechargeEntity.setActivityId(activityEntity.getActivityId());
        activityRechargeEntity.setActivityName(activityEntity.getActivityName());
        activityRechargeEntity.setStrategyId(activityEntity.getStrategyId());
        activityRechargeEntity.setRechargeId(RandomStringUtils.randomNumeric(12));
        activityRechargeEntity.setRechargeTime(new Date());
        activityRechargeEntity.setTotalCount(activityCountEntity.getTotalCount());
        activityRechargeEntity.setDayCount(activityCountEntity.getDayCount());
        activityRechargeEntity.setMonthCount(activityCountEntity.getMonthCount());
        activityRechargeEntity.setPayAmount(activitySkuEntity.getProductAmount());
        activityRechargeEntity.setBizId(skuRechargeEntity.getBizId());

        // 构建聚合对象
        return CreateQuotaOrderAggregate.builder()
                .userId(skuRechargeEntity.getUserId())
                .activityId(activitySkuEntity.getActivityId())
                .totalCount(activityCountEntity.getTotalCount())
                .dayCount(activityCountEntity.getDayCount())
                .monthCount(activityCountEntity.getMonthCount())
                .activityRechargeEntity(activityRechargeEntity)
                .build();
    }

    @Override
    public ActivitySkuStockKeyVO takeQueueValue(Long sku) throws InterruptedException {
        return activityRepository.takeQueueValue(sku);
    }

    @Override
    public void clearQueueValue(Long sku) {
        activityRepository.clearQueueValue(sku);
    }

    @Override
    public void updateActivitySkuStock(Long sku) {
        activityRepository.updateActivitySkuStock(sku);
    }

    @Override
    public void clearActivitySkuStock(Long sku) {
        activityRepository.clearActivitySkuStock(sku);
    }

    @Override
    public int queryActivitySkuSurplus(Long sku) {
        return activityRepository.queryActivitySkuSurplus(sku);
    }

    @Override
    public List<Long> querySkuList() {
        return activityRepository.querySkuList();
    }

    @Override
    public Integer queryRaffleActivityAccountPartakeCount(Long activityId, String userId, String awardLockCheckFlag) {
        switch (UserAccountCountFlagVO.getByCode(awardLockCheckFlag)){
            case TOTAL:
                return activityRepository.queryRaffleActivityAccountTotalPartakeCount(activityId, userId);
            case MONTH:
                return activityRepository.queryRaffleActivityAccountMonthPartakeCount(activityId, userId);
            case DAY:
                return activityRepository.queryRaffleActivityAccountDayPartakeCount(activityId, userId);
            default:
                return 0;
        }
    }

    @Override
    public void updateRechargeOrder(DeliveryOrderEntity deliveryOrderEntity) {
        activityRepository.updateRechargeOrder(deliveryOrderEntity);
    }
}
